<template>
  <div class="goods-comment">
    <div class="head" v-if="commentInfo">
      <div class="data" >
        <p><span>{{ commentInfo.salesCount }}</span><span>人购买</span></p>
        <p><span>{{ commentInfo.praisePercent }}</span><span>好评率</span></p>
      </div>
      <div class="tags">
        <div class="dt">大家都在说：</div>
        <div class="dd" >
          <a href="javascript:;" v-for="(item,index) in commentInfo.tags" :key="index" :class="{active: tagsIndex === index}" @click="tagsClickIndex(index)">{{ item.title }} ({{ item.tagCount }})</a>
        </div>
      </div>
    </div>
    <div class="sort" >
      <span>排序：</span>
      <a href="javascript:;" :class="{active: reParams.sortField === null}" @click="changeSort(null)">默认</a>
      <a href="javascript:;" :class="{active: reParams.sortField === 'praiseCount'}" @click="changeSort('praiseCount')">最新</a>
      <a href="javascript:;" :class="{active: reParams.sortField === 'createTime'}" @click="changeSort('createTime')">最热</a>
    </div>

    <!-- 评论区域 -->
    <div class="list" v-if="commentList">
      <div class="item" v-for="CmListItem in commentList" :key="CmListItem.id">
        <div class="user">
          <img :src="CmListItem.member.avatar" alt="">
          <span>{{ formatNickname(CmListItem.member.nickname) }}</span>
        </div>
        <div class="body">
          <div class="score">
            <i class="iconfont icon-wjx01" v-for="i in CmListItem.score" :key="i + 'k'"></i>
            <i class="iconfont icon-wjx02" v-for="i in 5 - CmListItem.score" :key="i + 'x'"></i>
            <span class="attr" v-for="(spaceItem, index) in CmListItem.orderInfo.specs" :key="index">{{spaceItem.name}}：{{ spaceItem.nameValue }}</span>
          </div>
          <div class="text">{{ CmListItem.content }}</div>
          <!-- 评论图片 -->
          <GoodsCommentImage :pictures="CmListItem.pictures"/>
          <div class="time">
            <span>{{ CmListItem.orderInfo.createTime }}</span>
            <span class="zan"><i class="iconfont icon-dianzan"></i>{{ CmListItem.praiseCount }}</span>
          </div>
        </div>
      </div>
      <XtxPagination v-if="total" @current-change="changePageFn" :total="total" :pageSize="reParams.pageSize" :currenPage="reParams.page"/>
    </div>
  </div>
</template>

<script>
import GoodsCommentImage from './goods-comment-image.vue'
import { findCommentInfoGoods, findGoodsCommentList } from '@/api/goods'
import { inject, reactive, ref, watch } from 'vue'
export default {
  name: 'GoodsComment',
  components: {
    GoodsCommentImage
  },
  setup () {
    const goodsList = inject('goods')
    const commentInfo = ref(null)
    findCommentInfoGoods(goodsList.value.id).then(data => {
      data.result.tags.unshift({
        title: '有图',
        tagCount: data.result.hasPictureCount,
        type: 'img'
      })
      data.result.tags.unshift({
        title: '全部评论',
        tagCount: data.result.evaluateCount,
        type: 'all'
      })
      // 在设置数据之前，tags数组前追加 有图tag  全部评价tag
      commentInfo.value = data.result
    })

    // 激活tag
    const tagsIndex = ref(0)
    const tagsClickIndex = (index) => {
      tagsIndex.value = index
      const tag = commentInfo.value.tags[index]
      if (tag.type === 'all') {
        reParams.hasPicture = null
        reParams.tag = null
      } else if (tag.type === 'img') {
        reParams.hasPicture = true
        reParams.tag = null
      } else {
        reParams.hasPicture = null
        reParams.tag = tag.title
      }
      reParams.page = 1
    }

    // 准备筛选条件
    const reParams = reactive({
      page: 1,
      pageSize: 10,
      hasPicture: null, // 是否有图
      tag: null, // 标签
      // 排序方式：praiseCount：热度 createTime：最新
      sortField: null
    })

    // 点击排序触发
    const changeSort = (sortField) => {
      reParams.sortField = sortField
      // 将页码重置为0
      reParams.page = 1
    }

    // 获取评论数据列表
    const commentList = ref([])
    // 总数量
    const total = ref(null)
    // 当筛选条件发现改变时，重新触发请求
    watch(reParams, () => {
      findGoodsCommentList(goodsList.value.id, reParams).then(data => {
        commentList.value = data.result.items
        total.value = data.result.counts
      })
    }, { immediate: true })

    // 定义转换数据的函数 (对应vue2.0的过滤器)
    // 过滤商品属性
    // const formatSpecs = (specs) => {
    //   return specs.reduce((p, c) => `${p} ${c.name}: ${c.nameValue}`)
    // }

    // 过滤名称 中间隔****
    const formatNickname = (nickname) => {
      return nickname.substr(0, 1) + '****' + nickname.substr(-1) // substr(-1) 是指截取最后一个
    }

    // 分页组件切换按钮时触发
    const changePageFn = (page) => {
      reParams.page = page
    }

    return { commentInfo, tagsClickIndex, tagsIndex, changeSort, reParams, commentList, formatNickname, total, changePageFn }
  }
}
</script>

<style lang="less" scoped>
.goods-comment {
  .head{
    display: flex;
    padding: 30px 0;
    .data {
    width: 340px;
    display: flex;
    padding: 20px;
    p{
      flex: 1;
      text-align: center;
      span {
        display: block;
        &:first-child {
          font-size: 32px;
          color: @priceColor;
        }
        &:last-child{
          color: #999;
        }
      }
    }
  }
  .tags {
    flex: 1;
    display: flex;
    border-left: 1px solid #f5f5f5;
    .dt {
      font-weight: bold;
      width: 100px;
      text-align: right;
      line-height: 42px;
    }
    .dd {
      flex: 1;
      display: flex;
      flex-wrap: wrap;
      >a {
        width: 132px;
        height: 42px;
        margin-left: 20px;
        margin-bottom: 20px;
        border-radius: 4px;
        border: 1px solid #e4e4e4;
        color: #999;
        text-align: center;
        line-height: 40px;
        &:hover {
          border-color: @xtxColor;
          background: lighten(@xtxColor, 50%);
          color: @xtxColor;
        }
        &.active {
          border-color: @xtxColor;
          background: @xtxColor;
          color: #fff;
        }
      }
    }
  }
}
  .sort {
    height: 60px;
    line-height: 60px;
    border-top: 1px solid #f5f5f5;
    border-bottom: 1px solid #f5f5f5;
    margin: 0 20px;
    color: #666;
    >span {
      margin-left: 20px;
    }
    >a {
      margin-left: 30px;
      &.active, &:hover {
        color: @xtxColor;
      }
    }
  }
  .list {
    padding: 0 20px;
    .item {
      display: flex;
      padding: 25px 10px;
      border-bottom: 1px solid #f5f5f5;
      .user {
        width: 160px;
        img {
          width: 40px;
          height: 40px;
          border-radius: 50%;
          overflow: hidden;
        }
        span {
          padding-left: 10px;
          color: #666;
        }
      }
      .body {
        flex: 1;
        .score {
          line-height: 40px;
        }
        .iconfont {
          color: #ff9240;
          padding-right: 3px;
        }
        .attr {
          padding-left: 10px;
          color: #666;
        }
        .text  {
          color: #666;
          line-height: 24px;
        }
        .time {
          color: #999;
          display: flex;
          justify-content: space-between;
          margin-top: 5px;
        }
      }
    }
  }
}
</style>
